package com.tools.developer.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.content.res.Resources;
import android.os.Environment;

public class Asset {

	public static String getAbsCacheDirPath(Context context) {
		return context.getCacheDir().getAbsolutePath();
	}

	public static String getAbsDbFileDirPath(Context context) {
		return context.getFilesDir().getAbsolutePath();
	}

	private static InputStream getInputStream(Context context, String name)
			throws IOException {

		File file = new File(getAbsDbFileDirPath(context) + "/" + name);
		return new FileInputStream(file);
	}

	public static InputStream getAsset(Context context, String asset)
			throws IOException {
		return getInputStream(context, asset);
	}

	public static String getExternalStorageDirectoryx() {
		File file = Environment.getExternalStorageDirectory();
		String apkCacheDir = file.getAbsolutePath() + "/kbatterydoctor";
		try {
			new File(apkCacheDir + "/").mkdirs(); // 如果没有路径，创建之
		} catch (Exception ex) {
		}
		return apkCacheDir;
	}

	public static String getDownloadPath() {
		File file = Environment.getExternalStorageDirectory();
		String apkCacheDir = file.getAbsolutePath()
				+ "/kbatterydoctor/download/";
		try {
			File downPath = new File(apkCacheDir);
			if (!downPath.exists()) {
				downPath.mkdirs(); // 如果没有路径，创建之
			}
		} catch (Exception ex) {
		}
		return apkCacheDir;

	}

	public static boolean copyFileFromRaw(int resId, File file, Context context) {
		Resources rs = context.getResources();
		if (file.exists()
				&& file.length() == rs.openRawResourceFd(resId).getLength())
			return false;
		FileOutputStream out = null;
		InputStream is = null;
		try {
			out = new FileOutputStream(file, false);
			is = context.getResources().openRawResource(resId);
			byte buf[] = new byte[1024];
			int len;
			while ((len = is.read(buf)) > 0) {
				out.write(buf, 0, len);
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				if (out != null)
					out.close();
				if (is != null)
					is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
